// rs_anwser/pages/subject/subject.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    collectPrompt: true,
    animation: {},
    current: 0,
    subjectIndex: 1,
    pageSize: 10,
    wrongCount: 0,
    rightCount: 0,
    total: 160,
    subjectList: [

    ],
    param: {
      subjectType: 'judge'
    }
  
  },

  updateCurrent: function (data) {
    this.setData({ current: data.detail });
  },
  updateSubjectList: function (data) {
    this.setData({ subjectList: data.detail });

  },
  updatePageIndex: function (data) {
    this.setData({ pageIndex: data.detail });
  },

  changeSubject:function(e){
    
    var me = this;
    this.setData({ current: e.detail.current, subjectIndex: e.detail.current + 1 });
    if (e.detail.current == this.data.subjectList.length - 1) {
      app.getData(this, "entry/wxapp/getSubjectList", { subjectType: 'judge' });
    }
  
  },

  /**
   * 选择题目选项
   */
  selectOption:function(e){
      var subject = this.getCurrentSubject();
      if(typeof(subject.result) == 'undefined'){
        //如果还没有没有答题
        var answer = subject.answer;
        var value = e.currentTarget.dataset.value;
       
          if (value == 1 && '正确'== answer) {
            subject.rightClass = 'result-right';
            this.setData({ rightCount: ++this.data.rightCount });
            subject.result = 1;
          } else {
            subject.wrongClass = 'result-wrong';
            this.setData({ wrongCount: ++this.data.wrongCount });
            subject.result = 2;
            app.addWrong('judge',subject.id);
          }
        this.updateCurrentSubject(subject);
      }

  },

  /**
   * 
   */
  collect: function (e) {
    app.collect('judge', e.currentTarget.dataset.id);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var me = this;
    app.getData(this, "entry/wxapp/getSubjectList", { subjectType: 'judge' });
    var date = new Date();
    var userInfo = wx.getStorageSync('userInfo');
    this.setData({
      refreshTime: date.toLocaleTimeString(),
      collectPrompt: userInfo.prompt.collect_prompt
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },


  /**
   * 获取题目
   */
  getCurrentSubject:function(){

    var subjectList = this.data.subjectList;
    var current = this.data.current;
    return subjectList[current];

  },

   /**
    * 更新当前题目状态
    */
  updateCurrentSubject:function(subject){
    var subjectList = this.data.subjectList;
    var current = this.data.current;
    subjectList[current] = subject;
    this.setData({subjectList:subjectList});
  }
})